<template>
	<view>
		<view class="img-view">
			<image :src="detail.data.img[0].image" mode="aspectFill"></image>
		</view>
		<view class="container b-f p-b">
			<view class="article-title">
				<text class="f-32">{{ detail.data[0].title }}</text>
			</view>
			<view class="article-little dis-flex flex-x-between m-top10">
				<view class="article-little__left">
					<text class="article-views f-24 col-8">{{ detail.time }}</text>
				</view>
				<view class="article-little__right">
					<text class="article-little__right-text f-24 col-8">{{ detail.data.dianzhan }}</text>
					<text class="article-little__right-image iconfont icon-dianzan" @tap="clickpraise" :id="[detail.data[0].id]"></text>
				</view>
			</view>

			<view class="article-little dis-flex flex-x-between m-top10">
				<view class="article-little__left">
					<text class="iconfont f-34 icon-map"></text>
					<text class="article-views f-24 col-8">{{ detail.data[0].city }}</text>
				</view>
			</view>

			<view class="article-content m-top20">
				<rich-text :nodes="article"></rich-text>
			</view>
		</view>

		<!-- 快捷导航 -->
		<shortcut></shortcut>
	</view>
</template>

<script>
	import shortcut from '@/components/goods/shortcut.vue'
	export default {
		components: {
			// （组件命名）
			shortcut
		},
		props: {
			shortcut: {}
		},
		data() {
			return {
				article: '',
				strings: '',
				// 文章详情
				detail: {},
				iszan: false,
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			// 获取文章详情
			this.getArticleDetail(options.id);
		},

		methods: {
				clickpraise(e) {
					let _id = e.currentTarget.id;
					let _this = this;
					
					if (_this.iszan){
						_this.showError('你已赞过该文章！');
						return;
					}
			
					_this._post_form('social.social/like', {
						s_id: _id,
					}, (result) => {
						if (result.code == 1) {
							_this.iszan = true;
							_this.detail.data.dianzhan++
						} else {
							_this.showError('网络错误！');
						}
					});
			
			},
			/**
			 * 获取文章详情
			 */
			getArticleDetail(article_id) {
				let _this = this;
				_this._post_form('social.social/details', {
					s_id: article_id,
				}, function(result) {

					console.log(result)
					let detail = result;



					detail.time = _this.transformTime(Number(result.data[0].time));
					// 富文本转码
					if (detail.data[0].content.length > 0) {
						let cont = _this.htmlDecodeByRegExp(detail.data[0].content);
						let article = cont.replace(/<img/g, "<img style='max-width:100%;height:auto;'");
						_this.setData({
							'article': article
						});
					}
					_this.setData({
						detail
					});
				});
			},

			/*2.用正则表达式实现html解码*/
			htmlDecodeByRegExp: function(str) {
				var s = "";
				if (str.length == 0) return "";
				s = str.replace(/&amp;/g, "&");
				s = s.replace(/&lt;/g, "<");
				s = s.replace(/&gt;/g, ">");
				s = s.replace(/&nbsp;/g, " ");
				s = s.replace(/&#39;/g, "\'");
				s = s.replace(/&quot;/g, "\"");
				return s;
			},

			/**
			 * 分享当前页面
			 */
			onShareAppMessage() {
				// 构建页面参数
				let params = _this.urlEncode({
					'article_id': this.data.detail.article_id,
					'referee_id': _this.getUserId()
				});
				return {
					title: this.data.detail.article_title,
					path: "/pages/article/detail/index?" + params
				};
			},
		}
	}
</script>

<style>
	/* wxParse.wxss */
	
	@import "../../utils/editpriconfont.css";

	page {
		background: #fff;
	}

	image,
	.img-view {
		width: 750upx;
		/* height: 500upx; */
	}
	
	.container {
		padding: 10upx;
	}

	.article-content {
		font-size: 28rpx;
	}


	.wxParse-img {
		width: 706upx;
	}
	.article-little__right{
		margin-right:5px;
	}
	.article-little__right-image {
		width: 40upx;
		height: 40upx;
	}
	.article-little__right-text{
		margin-right:5px;
	}
</style>
